/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun<iot25@lingyun>
 *                  All rights reserved.
 *
 *       Filename:  sqlite.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(24/04/25)
 *         Author:  LingYun <iot25@lingyun>
 *      ChangeLog:  1, Release initial version on "24/04/25 14:31:30"
 *                 
 ********************************************************************************/

#include "sqlite.h"



int init_database()
{
    char *err_msg = NULL;
    int rc;
    
    // 打开或创建数据库
    rc = sqlite3_open("temperature_server.db", &g_db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(g_db));
        sqlite3_close(g_db);
        return -1;
    }
    
    // 创建表（如果不存在）
    const char *sql = "CREATE TABLE IF NOT EXISTS temperature_records("
                      "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                      "device_sn TEXT NOT NULL,"
                      "timestamp INTEGER NOT NULL,"
                      "temperature REAL NOT NULL,"
                      "receive_time INTEGER NOT NULL);";
                      
    rc = sqlite3_exec(g_db, sql, NULL, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", err_msg);
        sqlite3_free(err_msg);
        return -1;
    }
}











